from mogView import *
from mogModel import *
from pydispatch import dispatcher
import wx

class Controller:

    def __init__(self, app):
        self.model = Model()
        self.view = View(None)
        self.view.grid.SetTable(MessagesTable(self.model.ringtongInfos))
        self.view.Bind(wx.EVT_CLOSE, self.OnClose)
        self.view.btStart.Bind(wx.EVT_BUTTON, self.startProcess)
        self.view.pageStart.Bind(wx.EVT_TEXT, self.updatePageStartModel)
        self.view.pageEnd.Bind(wx.EVT_TEXT, self.updatePageEndModel)
        self.view.category.Bind(wx.EVT_COMBOBOX, self.updateCategoryModel)
        self.view.startId.Bind(wx.EVT_TEXT, self.updateStartIdModel)
        self.view.btExport.Bind(wx.EVT_BUTTON, self.export)
        dispatcher.connect(self.onErrorHandler, signal='MSG_NET_ERROR')
        dispatcher.connect(self.onTagParseComplite, signal='MSG_TAG_PARSE_COMPLITE')
        self.view.Show()
        wx.CallAfter(dispatcher.send,'MSG_STATE_CHANGE', dispatcher.Anonymous, 0)



    def onTagParseComplite(self):
        wx.CallAfter(dispatcher.send,'MSG_STATE_CHANGE', dispatcher.Any, 6)



    def onErrorHandler(self, data):
        if data == -1:
            wx.CallAfter(self.view.showMessage,'ERROR', u'\u7f51\u7edc\u9519\u8bef\uff0c\u65e0\u6cd5\u89e3\u6790\u7f51\u9875')
            wx.CallAfter(self.view.setContinueView)
        elif data == -2:
            wx.CallAfter(self.view.showMessage,'ERROR', u'\u7f51\u7edc\u9519\u8bef\uff0c\u65e0\u6cd5\u4e0b\u8f7d\u6587\u4ef6')
            wx.CallAfter(self.view.setContinueView)
        elif data == -3:
            wx.CallAfter(self.view.showMessage,'ERROR', u'\u7f51\u7edc\u9519\u8bef\uff0c\u89e3\u6790Tag')
            wx.CallAfter(self.view.setContinueView)



    def OnClose(self, evt):
        self.model.stopThreads()
        self.view.Destroy()



    def export(self, evt):
        filename = self.model.export()
        wx.CallAfter(self.view.showMessage,u'\u63d0\u793a', u'\u6210\u529f\u5bfc\u51fa' + filename)



    def updateStartIdModel(self, evt):
        if self.view.startId.GetValue().isdigit():
            self.model.startId = int(self.view.startId.GetValue())



    def updateCategoryModel(self, evt):
        self.model.category = self.view.category.GetValue()



    def updatePageEndModel(self, evt):
        if self.view.pageEnd.GetValue().isdigit():
            self.model.pageEnd = int(self.view.pageEnd.GetValue())



    def updatePageStartModel(self, evt):
        if self.view.pageStart.GetValue().isdigit():
            self.model.pageStart = int(self.view.pageStart.GetValue())



    def startProcess(self, evt):
        if not self.view.startId.GetValue().isdigit() or not self.view.pageStart.GetValue().isdigit():
            wx.CallAfter(self.view.showMessage,'ERROR', u'\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u5f00\u59cb\u9875\u548cID')
            return 
        if self.model.isDone():
            self.model.clear()
        self.model.execute()



    def onStopProcess(self, evt):
        self.model.stopThreads()




